package switchviz.network;

import java.io.IOException;

import org.apache.log4j.Logger;
import org.jdom.JDOMException;

import switchviz.core.ResourceManager;
import switchviz.core.SwitchVizException;
import switchviz.core.signal.OIDSignal;
import switchviz.network.netswitch.Switch;
import switchviz.network.netswitch.SwitchDataModel;
import switchviz.network.netswitch.SwitchTree;
import switchviz.network.netswitch.SwitchXMLFilter;
import switchviz.network.snmp.SnmpStatus;

public class NetworkHandShake
{
	public static final String SYSNAME = "1.3.6.1.2.1.1.5.0";
	public static final String SYSDESCR = "1.3.6.1.2.1.1.1.0";
	public static final String SYSUPTIME = "1.3.6.1.2.1.1.3.0";
	public static final String IFNUMBER = "1.3.6.1.2.1.2.1.0";
	public static final int BADCONNECTION = 0;
	public static final int NOCONNECTION = -1;
	public static final int CONNECTION = 1;
	public static final int FIRSTCONNECTION = 2;
	private static Logger logger = Logger.getLogger(NetworkHandShake.class);
	private Switch _netswitch = null;
	private SwitchDataModel _switchmodel = null;
	private OIDSignal _siguptime = null;
	private OIDSignal _sigtype = null;
	
	public NetworkHandShake(SwitchDataModel switchmodel) throws SwitchVizException
	{
		_switchmodel = switchmodel;
		_netswitch = new Switch(_switchmodel);
		addSignals();
	}
	
	public SwitchDataModel getSwitchModel()
	{
		return _switchmodel;
	}
	
	public void startHandShake() throws InterruptedException, NetworkException
	{
		if(logger.isInfoEnabled())
			logger.info("Handshake initiated...");
		_netswitch.send();
	}
	
	public void close() throws NetworkException
	{
		if(logger.isInfoEnabled())
			logger.info("Handshake closed.");
		_netswitch.close();
	}
	
	public void getPortList() throws NetworkException, JDOMException, IOException
	{
		_switchmodel.setPortList(NetworkFacade.getNumberOfPorts(_switchmodel));
		_switchmodel.setType(NetworkFacade.getSwitchType(_switchmodel));
		SwitchTree switchtree = new SwitchTree(ResourceManager.getInstance().getSwitchConfigurationFile(), new SwitchXMLFilter());
		switchtree.changeSwitchNode(_switchmodel);
		switchtree.saveXML();
	}
	
	public int processStatus()
	{
		SnmpStatus status = _netswitch.getNetworkStatus();
		int stat = -1;
		if(status.getSendPduCount() == 1 && status.getReceivedPduCount() == 1)
		{
			stat = 1;
			_switchmodel.setUpTime(Long.valueOf(_siguptime.getData().getValue().toString()));
			if(!_switchmodel.getType().equals(_sigtype.getData().getValue().toString()))
			{
				stat = 2;
			}
			if(logger.isDebugEnabled())
				logger.debug("Switch Type: " + _sigtype.getData().getValue().toString());
		}
		else
		{
			if(logger.isDebugEnabled())
				logger.debug("Handshake failed with Switch " + _netswitch.getSwitchDataModel().getName() + " @ " + _netswitch.getSwitchDataModel().getAddress());
			stat = -1;
		}
		
		return stat;
	}
	
	private void addSignals() throws SwitchVizException
	{
		OIDSignal signame = new OIDSignal("1.3.6.1.2.1.1.5.0");
		signame.setAliasName("sysName");
		
		_sigtype = new OIDSignal("1.3.6.1.2.1.1.1.0");
		signame.setAliasName("sysDescr");
		
		_siguptime = new OIDSignal("1.3.6.1.2.1.1.3.0");
		signame.setAliasName("sysUpTime");
		
		OIDSignal sigifnumber = new OIDSignal("1.3.6.1.2.1.2.1.0");
		signame.setAliasName("ifNumber");
		
		_netswitch.add(signame);
		_netswitch.add(_sigtype);
		_netswitch.add(_siguptime);
		_netswitch.add(sigifnumber);
	}
}
